﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Threading;
using System.Data.SqlClient;
using System.Data;

namespace MasterServer {
	//Client - Master Server protocol - Server version
	public class CMSProtocolS : ServerProtocol {
		private static int nextPort = 8181;
		private static TcpListener NextListener {
			get {
				TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), nextPort);
				try {
					tcpListener.Start();
				} catch (SocketException) {
					nextPort++;
					return NextListener;
				}
				nextPort++;
				return tcpListener;
			}
		}
		internal bool Listen() {
			while (true) {
				AcceptSocket();
				if (Receive() == "czesc") {
					TcpListener listener = NextListener;
					new Thread(new ClientConnection(listener).Run).Start();
					Send("czesc " + (listener.LocalEndpoint as IPEndPoint).Port);
				} else {
					Send("Unrecognized command.");
				}
			}
		}
		class ClientConnection : ServerProtocol {
			bool authenticated = false;
			private string MozliweKomunikaty = "login" + " " + "rejestracja" + " " + "ilemampkt" + " " + "mojeteksty" + " " + "szczegolytekstu" + " " + "<TekstID>" + " " + "serwerjezykowy" + " " + "help" + " " + "quit";
			internal ClientConnection(TcpListener listener) {
				tcpListener = listener;
			}
			internal void Run() {
				AcceptSocket();
				if (Receive() == "OK") {
					Send("OK");
					Listen();
				} else {
					Send("Error. Connection closed.");
					return;
				}
			}
			//funkcja do obsługi połączenia z klientem
			internal void Listen() {
				string msg;
				while (true) {
					msg = Receive();
					string[] napisy = msg.Split(' ');
					switch (napisy[0]) {
						case "login":
							Send("login OK");
							string login = napisy[1];
							msg = Receive();
							napisy = msg.Split(' ');
							if (napisy[0] == "password") {
								if (Authenticate(login, napisy[1])) {
									Send("Zalogowano jako " + login);
								} else {
									Send("Bledny login/haslo");
								}
							} else {
								Send("Bledna komenda");
							}
							break;
						case "ilemampkt":
							if (Authenticated()) {
								Send("Punkty: 123");
							}
							break;
						case "mojeteksty":
							if (Authenticated()) {
								Send("teksty: 1");
							}
							break;
						case "szczegolytekstu":
							if (Authenticated()) {
								Send("brak szczeguuf");
							}
							break;
						case "serwerjezykowy":
							if (Authenticated()) {
								Send("Brak serwera");
							}
							break;
						case "rejestracja":
							var table = new DataTable("Logowanie");
							if (Authenticated()) {
								Send("Bl");
							}
							break;
						case "quit":
							Send("Connection closed");
							Disconnect();
							return;
						case "help":
							Send("Pomoc");
							Send(MozliweKomunikaty);
							break;
						default:
							Send(MozliweKomunikaty);
							break;
					}
				}
			}

			private bool Authenticated() {
				if (authenticated) {
					return true;
				} else {
					Send("Zaloguj / zarejestruj");
					return false;
				}
			}

			private bool Authenticate(string login, string p) {
				authenticated = true;
				return true;
			}
		}
	}
}
